<template>
  <scroll class="listview" :data='data' :listenScroll="listenScroll" :probeType='probeType' @scroll="scroll" ref="listview">
    <ul>
      <li v-for="group in data" :key='group.title' class="list-group" ref="listGroup">
        <h2 class="list-group-title">{{group.title}}</h2>
        <ul>
          <li v-for="item in group.items" :key="item.id" @click="selectItem(item)" class="list-group-item">
            <img v-lazy="item.imgUrl" :alt="item.name" class="avatar">
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <div class="list-shortcut" @touchstart="onShortcutTouchStart" @touchmove.stop.prevent="onShortcutTouchMove">
      <ul>
        <li class="item" v-for="(item,index) in shortcutList" :key="index" :data-index="index" :class="{'current': currentIndex==index}">{{item}}</li>
        <!-- currentIndex == index? 'current' : '' -->
      </ul>
    </div>
    <div class="list-fixed" v-show="fixedTitle" ref="fixed">
      <h2 class="fixed-title">{{fixedTitle}}</h2>
    </div>
    <div v-show="!data.length" class="loading-container">
      <loading></loading>
    </div>
  </scroll>
</template>

<script>
  import Scroll from "base/scroll/scroll";
  import Loading from 'base/loading/loading';
  import {
    getDataIndex
  } from "common/js/dom";
  
  const ANCHOR_HEIGHT = 18
  const TITLE_HEIGHT = 30

  export default {
    name: "ListView",
    props: {
      data: {
        type: Array,
        default: []
      }
    },
    components: {
      Scroll,
      Loading
    },
    computed: {
      shortcutList() {
        return this.data.map(group => {
          //substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
          // 字符串截断
          return group.title.substr(0, 1);
        });
      },
      fixedTitle() {
        if (this.scrollY > 0) {
          return ''
        }
        return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
      }
    },
    data() {
      return {
        scrollY: -1,
        currentIndex: 0,
        diff: -1
      }
    },
    methods: {
      // 公共方法
      selectItem(item) {
        this.$emit('select', item)
      },
      onShortcutTouchStart(e) {
        let anchorIndex = getDataIndex(e.target, "index");
        let firstTouch = e.touches[0]
        this.touch.y1 = firstTouch.pageY
        this.touch.anchorIndex = anchorIndex
        this._scrollTo(anchorIndex)
      },
      onShortcutTouchMove(e) {
        let firstTouch = e.touches[0]
        this.touch.y2 = firstTouch.pageY
        let delate = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
        let anchorIndex = delate + parseInt(this.touch.anchorIndex)
        this._scrollTo(anchorIndex)
      },
      scroll(pos) {
        this.scrollY = pos.y
      },
      refresh(){
        this.$refs.listview.refresh()
      },
      // 私有方法
      _scrollTo(index) {
        if (!index && index !== 0) {
          return
        }
        if (index < 0) {
          index = 0
        } else if (index > this.listHeight.length - 2) {
          index = this.listHeight.length - 2
        }
        this.scrollY = -this.listHeight[index]
        this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0)
      },
      _calculateHeight() {
        this.listHeight = []
        const list = this.$refs.listGroup
        let varHeight = 0;
        this.listHeight.push(varHeight)
        for (let i = 0; i < list.length; i++) {
          // 得到每个元素
          let item = list[i]
          varHeight += item.clientHeight
          this.listHeight.push(varHeight)
        }
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this._calculateHeight()
        }, 20)
      },
      scrollY(newY) {
        const listHeight = this.listHeight;
        // 开头
        if (newY > 0) {
          this.currentIndex = 0;
          return
        }
        // 中间
        for (let i = 0; i < listHeight.length - 1; i++) {
          let heinght1 = listHeight[i]
          let heinght2 = listHeight[i + 1]
          if (-newY >= heinght1 && -newY < heinght2) {
            this.currentIndex = i;
            this.diff = heinght2 + newY
            return
          }
        }
        // 结尾
        this.currentIndex = listHeight.length - 2;
      },
      diff(newVal) {
        let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
        if (this.fixedTop === fixedTop) {
          return
        }
        this.fixedTop = fixedTop
        this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`
      }
    },
    created() {
      // 这里创建就不会被监听
      this.touch = {}
      this.listenScroll = true
      this.listHeight = []
      this.probeType = 3
    },
  };

</script>
